<?php

namespace app\mobile\controller\consult\index;

use app\BaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 未读记录
     * @return bool|string
     */
    public function unreadRecords(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $where[] = ['uid','=',$this->userData['id']];
            $where[] = ['user_dot','=',1];
            $data = $ConsultOnlineModel->field('id,uid,consult_id,question_id,question,time,1 as type')->where($where)->with([
                'user',
                'consult',
                'questionName'
            ])->select()->toArray();
            $data2 = $ConsultOfflineModel->field('id,uid,consult_id,question_id,question,time,2 as type')->where($where)->with([
                'user',
                'consult',
                'questionName'
            ])->select()->toArray();
            $data3 = array_merge($data,$data2);
            $field = array_column($data3,'time');
            array_multisort($field, SORT_DESC, $data3);
            $data4 = array_slice($data3, 0, 4);
            return success($data4);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 主页数据
     * @return bool|string
     */
    public function data(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $online_num = $ConsultOnlineModel->where('uid','=',$this->userData['id'])->count();
            $offline_num = $ConsultOfflineModel->where('uid','=',$this->userData['id'])->count();
            return success([
                'online_num'=>$online_num,
                'offline_num'=>$offline_num
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}